package cn.hjh.dao;

import cn.hjh.pojo.Users;
import org.hibernate.Query;


import java.io.Serializable;
import java.util.List;

public class UsersDao {

    public Users get(Serializable id){
        return (Users) HibernateUtil.currentSession().get(Users.class,id);
    }

    public void save(Users users){
        HibernateUtil.currentSession().save(users);
    }

    public void delete(Users users){
        HibernateUtil.currentSession().delete(users);
    }

    public Users merge(Users users){
        return (Users) HibernateUtil.currentSession().merge(users);
    }

    /* HQL查询用户表的所有用户 */
    public List<Users> findAll(){
        String hql = "from Users";
        Query query = HibernateUtil.currentSession().createQuery(hql);
        return  query.list();
    }

    /*  查询用户名是。。。的用户 */
    public List<Users> findUsersByName(String name){
        String hql = "from Users as users where users.name = ?";
        Query query = HibernateUtil.currentSession().createQuery(hql);
        query.setString(0,name);
        return query.list();
    }

    /* 分页显示用户信息 */
    public List<Users> findUsersByPage(int pageNo,int pageSize){
        return HibernateUtil.currentSession().createQuery("from Users order by id").setFirstResult((pageNo - 1) * pageSize) //起始下标
        .setMaxResults(pageSize) //设置返回最大结果数
        .list();
    }
}
